<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>geo_distance 聚合 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'search-aggregations-bucket-geodistance-aggregation.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-bucket-geodistance-aggregation.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/search-aggregations-bucket-geodistance-aggregation.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/search-aggregations-bucket-geodistance-aggregation.html" rel="nofollow" target="_blank">../en/search-aggregations-bucket-geodistance-aggregation.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations.html">聚合</a></span>
»
<span class="breadcrumb-link"><a href="search-aggregations-bucket.html">桶(bucket)聚合</a></span>
»
<span class="breadcrumb-node">geo_distance 聚合</span>
</div>
<div class="navheader">
<span class="prev">
<a href="search-aggregations-bucket-filters-aggregation.html">« filters 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-bucket-geohashgrid-aggregation.html">geohash_grid 聚合 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="search-aggregations-bucket-geodistance-aggregation"></a>地理距离(geo_distance)聚合
</h2>
</div></div></div>
<p>
一个多桶聚合，作用于 <code class="literal">geo_point</code> 字段，在概念上与<a class="xref" href="search-aggregations-bucket-range-aggregation.html" title="range聚合" rel="nofollow">range</a>聚合非常相似。

用户可以定义一个原点和一组距离范围桶。

该聚合计算每个文档值与原点的距离，并根据范围确定其所属的桶(如果文档与原点之间的距离在该桶的距离范围内，则该文档属于该桶)。
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT /museums
{
    "mappings": {
        "properties": {
            "location": {
                "type": "geo_point"
            }
        }
    }
}

POST /museums/_bulk?refresh
{"index":{"_id":1}}
{"location": "52.374081,4.912350", "name": "NEMO Science Museum"}
{"index":{"_id":2}}
{"location": "52.369219,4.901618", "name": "Museum Het Rembrandthuis"}
{"index":{"_id":3}}
{"location": "52.371667,4.914722", "name": "Nederlands Scheepvaartmuseum"}
{"index":{"_id":4}}
{"location": "51.222900,4.405200", "name": "Letterenhuis"}
{"index":{"_id":5}}
{"location": "48.861111,2.336389", "name": "Musée du Louvre"}
{"index":{"_id":6}}
{"location": "48.860000,2.327000", "name": "Musée d'Orsay"}

POST /museums/_search?size=0
{
    "aggs" : {
        "rings_around_amsterdam" : {
            "geo_distance" : {
                "field" : "location",
                "origin" : "52.3760, 4.894",
                "ranges" : [
                    { "to" : 100000 },
                    { "from" : 100000, "to" : 300000 },
                    { "from" : 300000 }
                ]
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/463.console"></div>
<p>响应：</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    ...
    "aggregations": {
        "rings_around_amsterdam" : {
            "buckets": [
                {
                    "key": "*-100000.0",
                    "from": 0.0,
                    "to": 100000.0,
                    "doc_count": 3
                },
                {
                    "key": "100000.0-300000.0",
                    "from": 100000.0,
                    "to": 300000.0,
                    "doc_count": 1
                },
                {
                    "key": "300000.0-*",
                    "from": 300000.0,
                    "doc_count": 2
                }
            ]
        }
    }
}</pre>
</div>
<p>
指定字段的类型必须是<code class="literal">geo_point</code> (只能在映射中显式设置)。

它还可以保存一个<code class="literal">geo_point</code>字段数组，在这种情况下，所有这些都将在聚合过程中考虑在内。

原点可以接受<a class="xref" href="geo-point.html" title="Geo-point datatype"><code class="literal" rel="nofollow">geo_point</code> 类型</a>支持的所有格式：
</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
对象格式： <code class="literal">{ "lat" : 52.3760, "lon" : 4.894 }</code> - 这是最安全的格式，因为它对<code class="literal">lat</code> 和 <code class="literal">lon</code>值最明确
</li>
<li class="listitem">
字符串格式：<code class="literal">"52.3760, 4.894"</code> - 第一个值是<code class="literal">lat</code>，第二个是<code class="literal">lon</code>
</li>
<li class="listitem">
数组格式：<code class="literal">[4.894, 52.3760]</code> - 基于<code class="literal">GeoJson</code>标准，第一个值是<code class="literal">lon</code>，第二个是<code class="literal">lat</code>
</li>
</ul>
</div>
<p>
默认情况下，距离的单位是 <code class="literal">m</code> (米)，但也接受：<code class="literal">mi</code> (英里), <code class="literal">in</code> (英尺), <code class="literal">yd</code> (码), <code class="literal">km</code> (千米), <code class="literal">cm</code> (厘米), <code class="literal">mm</code> (毫米).
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /museums/_search?size=0
{
    "aggs" : {
        "rings" : {
            "geo_distance" : {
                "field" : "location",
                "origin" : "52.3760, 4.894",
                "unit" : "km", <a id="CO227-1"></a><i class="conum" data-value="1"></i>
                "ranges" : [
                    { "to" : 100 },
                    { "from" : 100, "to" : 300 },
                    { "from" : 300 }
                ]
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/464.console"></div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO227-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>距离将以千米为单位计算</p>
</td>
</tr>
</table>
</div>
<p>
有两种距离计算模式：<code class="literal">arc</code> (默认) 和 <code class="literal">plane</code>。

<code class="literal">arc</code>计算是最精确的，<code class="literal">plane</code> 最快但最不精确。

当搜索环境“狭窄”，并且跨越较小的地理区域(约5公里)时，请考虑使用<code class="literal">plane</code>。

对于非常大的区域的搜索(例如跨洲搜索)，<code class="literal">plane</code>将返回更高的误差。。

可以使用参数<code class="literal">distance_type</code>设置距离计算类型：
</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /museums/_search?size=0
{
    "aggs" : {
        "rings" : {
            "geo_distance" : {
                "field" : "location",
                "origin" : "52.3760, 4.894",
                "unit" : "km",
                "distance_type" : "plane",
                "ranges" : [
                    { "to" : 100 },
                    { "from" : 100, "to" : 300 },
                    { "from" : 300 }
                ]
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/465.console"></div>
<div class="section">
<div class="titlepage"><div><div>
<h3 class="title">
<a id="_keyed_response_5"></a>keyed 响应
</h3>
</div></div></div>
<p>将 <code class="literal">keyed</code> 标志设置为 <code class="literal">true</code>会将唯一的字符串键与每个桶相关联，并将范围作为哈希而不是数组返回：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /museums/_search?size=0
{
    "aggs" : {
        "rings_around_amsterdam" : {
            "geo_distance" : {
                "field" : "location",
                "origin" : "52.3760, 4.894",
                "ranges" : [
                    { "to" : 100000 },
                    { "from" : 100000, "to" : 300000 },
                    { "from" : 300000 }
                ],
                "keyed": true
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/466.console"></div>
<p>响应：</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    ...
    "aggregations": {
        "rings_around_amsterdam" : {
            "buckets": {
                "*-100000.0": {
                    "from": 0.0,
                    "to": 100000.0,
                    "doc_count": 3
                },
                "100000.0-300000.0": {
                    "from": 100000.0,
                    "to": 300000.0,
                    "doc_count": 1
                },
                "300000.0-*": {
                    "from": 300000.0,
                    "doc_count": 2
                }
            }
        }
    }
}</pre>
</div>
<p>也可以为每个范围定制一个键：</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">POST /museums/_search?size=0
{
    "aggs" : {
        "rings_around_amsterdam" : {
            "geo_distance" : {
                "field" : "location",
                "origin" : "52.3760, 4.894",
                "ranges" : [
                    { "to" : 100000, "key": "first_ring" },
                    { "from" : 100000, "to" : 300000, "key": "second_ring" },
                    { "from" : 300000, "key": "third_ring" }
                ],
                "keyed": true
            }
        }
    }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/467.console"></div>
<p>响应：</p>
<div class="pre_wrapper lang-console-result">
<pre class="programlisting prettyprint lang-console-result">{
    ...
    "aggregations": {
        "rings_around_amsterdam" : {
            "buckets": {
                "first_ring": {
                    "from": 0.0,
                    "to": 100000.0,
                    "doc_count": 3
                },
                "second_ring": {
                    "from": 100000.0,
                    "to": 300000.0,
                    "doc_count": 1
                },
                "third_ring": {
                    "from": 300000.0,
                    "doc_count": 2
                }
            }
        }
    }
}</pre>
</div>
</div>

</div>
<div class="navfooter">
<span class="prev">
<a href="search-aggregations-bucket-filters-aggregation.html">« filters 聚合</a>
</span>
<span class="next">
<a href="search-aggregations-bucket-geohashgrid-aggregation.html">geohash_grid 聚合 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>